package com.jxpanda.spring.module.wechat.component.http.service;


import com.jxpanda.spring.module.wechat.component.http.base.constant.MiniProgramApi;
import com.jxpanda.spring.module.wechat.component.http.base.request.query.MiniProgramQuery;
import com.jxpanda.spring.module.wechat.component.http.base.response.MiniProgramResponse;
import com.jxpanda.spring.module.wechat.config.properties.AppletProperties;
import com.jxpanda.spring.module.wechat.config.properties.WechatConfigProperties;
import com.jxpanda.spring.module.wechat.constant.WechatAppletType;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

/**
 * @author Panda
 */
@Service
@ConditionalOnProperty(prefix = "wechat.mini-program", name = "enabled", havingValue = "true")
public class MiniProgramReactiveHttpService extends WechatReactiveHttpService {

    public MiniProgramReactiveHttpService(WechatConfigProperties wechatConfigProperties) {
        super(wechatConfigProperties);
    }

    public Mono<MiniProgramResponse.AccessToken> getAccessToken(String appName) {
        AppletProperties appletProperties = wechatConfigProperties.getAppletProperties(WechatAppletType.MINI_PROGRAM, appName);
        return request(MiniProgramApi.GET_ACCESS_TOKEN.builder()
                .query(MiniProgramQuery.AccessToken.builder()
                        .appId(appletProperties.getAppId())
                        .secret(appletProperties.getAppSecret())
                        .build())
                .build());
    }

    public Mono<MiniProgramResponse.JsCode2Session> jsCode2Session(String appName, String code) {
        AppletProperties appletProperties = wechatConfigProperties.getAppletProperties(WechatAppletType.MINI_PROGRAM, appName);
        return request(MiniProgramApi.JS_CODE_2_SESSION.builder()
                .query(MiniProgramQuery.JsCode2Session.builder()
                        .appId(appletProperties.getAppId())
                        .secret(appletProperties.getAppSecret())
                        .jsCode(code)
                        .build())
                .build());
    }

}
